package com.hnyyjsxy.service.impl;

import com.hnyyjsxy.dao.StudentDao;
import com.hnyyjsxy.dao.impl.StudentDaoImpl;
import com.hnyyjsxy.domain.ApplyFor;
import com.hnyyjsxy.domain.Student;
import com.hnyyjsxy.domain.Violate;
import com.hnyyjsxy.service.StudentService;
import com.hnyyjsxy.util.MailUtils;

import java.util.Collections;
import java.util.List;

/**
 * @date 2021/02/04
 */
public class StudentServiceImpl implements StudentService {
    //定义dao层对象
    StudentDao studentDao = new StudentDaoImpl();

    /**
     * 通过学号查找学生信息
     * @param sid 学号
     * @return 学生实体类对象
     */
    @Override
    public Student getStudentBySid(String sid){
        Student student = studentDao.getStudentBySid(sid);
        return student;
    }

    /**
     * 通过学号查询该学生的违纪情况
     * @param sid 学号
     * @return 违纪情况list集合
     */
    @Override
    public List<Violate> queryDeregulation(String sid) {
        //1. 调用dao得到该学生的违纪情况list集合
        List<Violate> violateList = studentDao.queryDeregulation(sid);
        //2. list集合反转 让最新的违纪情况摆在最前面
        Collections.reverse(violateList);
        //3. 返回
        return violateList;
    }

    /**
     * 学生申请调换宿舍
     * @param stu 学生对象
     * @param room 申请房间号
     * @param bed 申请床号
     */
    @Override
    public void studentApply(Student stu, String room, String bed) {
        //1. 创建ApplyFor实体类对象，构造方法中进行赋值
        ApplyFor applyFor = new ApplyFor(stu.getSid(), stu.getBuilding(), stu.getRoom(), stu.getBed(), room, bed, 0);
        //2. 将ApplyFor实体类对象传给持久化层
        studentDao.studentApply(applyFor);
    }

    @Override
    public void alterCharge(Student student, double money) {
        student.setCharge(student.getCharge() + money);
        studentDao.alterCharge(student);
    }

    @Override
    public void inform(Student student) {
        MailUtils.sendMail(student.getEmail(),"您的电费已充值成功，现余额："+student.getCharge(),student.getSname()+"充值电费通知");
    }
}
